package com.jolink.omiis.wechat.api.impl;

import java.io.File;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jolink.omiis.wechat.SpringContextUtil;
import com.jolink.omiis.wechat.WeChatContext;
import com.jolink.omiis.wechat.keys.IPreferences;
import com.jolink.omiis.wechat.util.GsonUtil;

/**
 * 微信 Api 基础服务接口
 * 
 * @author WangYanqing
 *
 */

@Component
public class WeChatApiBaseService {

	@Autowired
	private WeChatContext context;

	protected <T> T getWeChatApiService(Class<?> clazz) {
		return SpringContextUtil.getBean(clazz);
	}

	protected Map<String, String> getComponentInfo() {
		return getContext().getCompentInfo();
	}

	protected String getCompanyAuthorizerAccessToken(String appId) {
		return getCompanyInfo(appId).get("authorizer_access_token");
	}

	protected Map<String, String> getCompanyInfo(String appId) {
		return getContext().getCompanyInfo(IPreferences.COMPANY_REDIS_PREFIX + "_" + appId);
	}

	@SuppressWarnings("rawtypes")
	public String executePost(String uri, String reqParams, String httpEntity) {
		return (String) getContext().getServiceUtil().getHttpHandler().post(uri, reqParams, httpEntity);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String executePost(String uri, Map<String, String> reqParams, String httpEntity) {
		return (String) getContext().getServiceUtil().getHttpHandler().post(uri, reqParams, httpEntity);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String executePost2(String uri, String reqParams, HttpEntity httpEntity) {
		return (String) getContext().getServiceUtil().getHttpHandler().post2(uri, reqParams, httpEntity);
	}

	/**
	 * 提交文件
	 * 
	 * @param uri
	 * @param reqParams
	 * @param httpEntity
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public String executePostFile(String uri, String reqParams, String filePath) {
		File file = new File(filePath);
		MultipartEntityBuilder meb = MultipartEntityBuilder.create();
		FileBody fileBody = new FileBody(file);
		meb.addPart("media", fileBody);
		HttpEntity httpEntity = meb.build();
		return (String) getContext().getServiceUtil().getHttpHandler().post(uri, reqParams, httpEntity);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Map<String, String> executeGet(String uri, String reqParams) {
		return (Map<String, String>) getContext().getServiceUtil().getHttpHandler().get(uri, reqParams);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String executeGet(String uri, Map<String, String> reqParams) {
		return (String) getContext().getServiceUtil().getHttpHandler().get(uri, reqParams);
	}

	public Map<String, String> result2Map(String jsonResult) {
		return GsonUtil.str2Map(jsonResult);
	}

	/**
	 * @return the context
	 */
	public WeChatContext getContext() {
		return context;
	}

	/**
	 * @param context
	 *            the context to set
	 */
	public void setContext(WeChatContext context) {
		this.context = context;
	}
}
